var app = getApp();
var common=require('../../../utils/common.js');
Page({
    data: {
        modalHidden: true,
        selectedAdd:'',
        selected:[],
        goodList:[],
        sum:'',
        arrTimeIndex: 0,
        addr: "",
        addressList: [],
        addrShow: false,
        scrollTop: 100,
        selectedID: -1,
        oinfo: {
            OrderSource: "all|web",
            Consignee: "",
            Cellphone: "",
           
            describe: "",
            gpsAddr: "",
            DeliveryDate: "",
            DeliveryTime: "",
            Payment: "",
            Remarks: "",
            TotalPrice: 0
        },
        dateStart: "2017-01-01",
        dateEnd: "2017-01-01"
    },
    buttonTap: function() {
        this.setData({
             modalHidden: false
        })
        },
        modalCandel: function() {
            // do something
            this.setData({
                 modalHidden: true
            })
            },
            modalConfirm: function() {
                let that=this;
                wx.showModal({
                    title:'是否已完成付款',
                    success(res){
                       if(res.confirm){
                            that.setData({
                                modalHidden: true
                           })
                           wx.request({
                             url: app.globalData.api+'addorder',
                             data:({
                                cart:JSON.stringify(that.data.selected),
                                selectedAdd:that.data.selectedAdd,
                                sum:that.data.sum,
                                openid:wx.getStorageSync('openid')
                             }),
                             success(){
                            wx.showModal({
                            title:'下单成功',
                            content:'请耐心等待商品配送\r\n点击确定前往个人中心',
                            showCancel:false,
                            success(res){
                                if(res.confirm){
                                    wx.switchTab({
                                      url: '../../my/my',
                                    })
                                }
                            }
                           })
                             }
                           })
                       
                        }
                  }

                })
               
               
                },
            
 
    bindTimeChange: function (e) {
        var _this = this;
        if (e.detail.value > 0) {
            _this.setData({
                arrTimeIndex: e.detail.value,
                "oinfo.DeliveryTime": _this.data.arrTime[e.detail.value]
            });
        }
    },
    bindDateChange: function (e) {
        this.setData({
            "oinfo.DeliveryDate": e.detail.value
        })
    },
    myaddrChange: function () {//触摸选择地址
        this.setData({ addrShow: true });
    },
    myaddrCancel: function () {//点击地址簿中取消按钮
        this.setData({ addrShow: false });
    },
    closeaddr:function(){//触摸遮罩层关闭地址选项
          this.setData({ addrShow: false });
    },
    updateAddress:function(e){
     
        const data = {
          describe:e.currentTarget.dataset.id.describe,
          gpsAddr:e.currentTarget.dataset.id.gpsAddr,
          id:e.currentTarget.dataset.id.id,
          name:e.currentTarget.dataset.id.name,
          phone:e.currentTarget.dataset.id.phone,
          userId:e.currentTarget.dataset.id.userId
        }
  
        console.log(e.currentTarget.dataset.id)
        wx.navigateTo({
          url: '../../my/updateAddress/updateAddress',
          success(res){
            res.eventChannel.emit('ccc',data)
          }
        })
      } ,
    toSelect: function (e) {//选中地址
        var _this = this;
        var id = e.currentTarget.dataset.aid;
        this.setData({
            selectedAdd:id
        })
        _this.setData({ selectedID: id });
        for (var i = 0; i < _this.data.addressList.length; i++) {
            if (_this.data.addressList[i].id == id) {
                _this.setData({     
                    "oinfo.id":_this.data.addressList[i].id,
                    "oinfo.userId":_this.data.addressList[i].userId,
                    "oinfo.describe": _this.data.addressList[i].describe,
                    "oinfo.name": _this.data.addressList[i].name,
                    "oinfo.phone": _this.data.addressList[i].phone,
                    "oinfo.gpsAddr": _this.data.addressList[i].gpsAddr,
                    addr: _this.data.addressList[i].gpsAddr + ' ' + _this.data.addressList[i].describe ,
                    addrShow: false
                });
                break;
            }
        }
    },
    onLoad(e){
      
        this.setData({
            selected:e.selected,
            sum:e.sum
        })
    },
    onShow: function (e) {
        let that=this;
        wx.request({
            url: app.globalData.api+'selectCartListsById?idlist='+JSON.stringify(that.data.selected),
            success(e){
               
                that.setData({
                    goodList:e.data.data
                })
            }
          })
        this.getAddressList();
       
    },
    getAddressList: function () {
        let that=this
        wx.request({
            url: app.globalData.api+'getAddressList?userid='+wx.getStorageSync('user').userId,
            success:function(res){
           
              that.setData({
                addressList:res.data.data
               
              })
              console.log(that.data.addressList)
            }
          })
    },


    getTotalPrice: function () {//应付金额
        var _this = this;
        var pl = _this.data.plist;//name: p.name, price: p.price, size: p.size, num: p.num, brand: p.brand,supplyno
        var alltotal = 0;
        for (var i = 0; i < pl.length; i++) {
            if (!isNaN(pl[i].price)) {
                alltotal += parseFloat(pl[i].price);
            }
        }
        this.setData({
            "oinfo.TotalPrice": alltotal
        });
    },
    getProductList: function () {
        var _this = this;
        var arr_pro = [];
        var pl = _this.data.plist;//name: p.name, price: p.price, size: p.size, num: p.num, brand: p.brand,supplyno
        for (var i = 0; i < pl.length; i++) {
            arr_pro.push({
                ProductName: pl[i].name,
                Price: pl[i].price,
                Size: pl[i].size,
                Num: pl[i].num,
                CakeNo: 0,
                OType: 0,
                IType: 0,
                SupplyNo: pl[i].supplyno,
                //生日内容
                IsCutting: 0,
                CutNum: 0,
                BrandCandleType: 0,
                Remarks: '',
                Premark: null,//生产备注
            });
        }
        return arr_pro;
    }
  
})